﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code;
using Web.Framework;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion.App
{
    public partial class CausaDesvioEdit : BasePage
    {
        public CausaDesvio causaDesvioCurrent
        {
            get
            {
                return (CausaDesvio)ViewState["CausaDesvio"];
            }
            set
            {
                ViewState["CausaDesvio"] = value;
            }
        }
      

        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.CAUSADESVIO_VER);
           
            if (!this.IsPostBack)
            {
                if (this.Request.QueryString["Id"] == null)
                {
                    causaDesvioCurrent = new CausaDesvio();
                }
                else
                {
                    causaDesvioCurrent = WebServiceHelper.GetCausasDeDesvioById(Convert.ToInt32(this.Request.QueryString["Id"]));
                }

                Bind();
            }
        }

        protected override void ApplySecurity()
        {
            this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {
            if (causaDesvioCurrent.Id != 0)
            {
                this.txtId.Text = Convert.ToString(causaDesvioCurrent.Id);

                this.txtNombre.Text = causaDesvioCurrent.Nombre;

                BindAuditory(causaDesvioCurrent);
            }
        }

        private void SetData()
        {
            causaDesvioCurrent.EmpresaId = BaseWebPage.CompanyId;
            causaDesvioCurrent.Nombre = this.txtNombre.Text; ;
        }

        private void Save()
        {
            causaDesvioCurrent = WebServiceHelper.SaveCausaDesvio(causaDesvioCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                Response.Redirect("/App/CausaDesvioList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("/App/CausaDesvioList.aspx", true);
        }        
    }
}